<?php
session_start();
include_once './c/config.php';
$access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appId.'&secret='.$secret.'&code='.$_GET['code'].'&grant_type=authorization_code';
$access = file_get_contents($access_token);
$accessObj = json_decode($access);
$openid   = $accessObj->openid;
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
$_SESSION['openid']=$openid;
$sql="select id from users where openid='".$openid."'  limit 0,1 ";
$query = $mysqli->query($sql);
if($query->num_rows>0){
}else{
    $redirect_uri = urlencode('http://mei94.com/dumpling/c.php');
    $api_userinfo ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo#wechat_redirect';
    header('Location: '.$api_userinfo);
    exit;
}
header('Location: home.php');
 
$mysqli->close();
exit;